<template>
  <div class="app-container" style="overflow-y: auto;">

    <el-row :gutter="24">
      <el-col :span="12">
        <el-card>
          <div id="cvBox_pie" style="width: 100%;height: 350px;">
          
          </div>
        </el-card>
      </el-col>

      <el-col :span="12">
        <el-card>
          <div id="cvBox_line" style="width: 100%;height: 350px;">
          
          </div>
        </el-card>
      </el-col>
    </el-row>


    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px" style="margin-top: 15px;">
      <el-form-item label="招聘ID" prop="recreitId">
        <el-input
          v-model="queryParams.recreitId"
          placeholder="请输入招聘ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="简历标题" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入简历标题"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="进度" prop="progress">
        <el-select v-model="queryParams.progress" placeholder="请选择进度" clearable>
          <el-option
            v-for="dict in dict.type.sys_cv_progress"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:cv:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:cv:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:cv:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:cv:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row> -->

    <el-table v-loading="loading" :data="cvList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="简历ID" align="center" prop="id" />
      <el-table-column label="招聘ID" align="center" prop="recruitId" />
      <el-table-column label="简历标题" align="center" prop="title" />
      <el-table-column label="简历" align="center" prop="file">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="handleDownload(scope.row.file)"
          >下载</el-button>
        </template>
      </el-table-column>
      <el-table-column label="进度" align="center" prop="progress">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_cv_progress" :value="scope.row.progress"/>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-check"
            @click="handleAccept(scope.row)"
            v-hasPermi="['system:cv:edit']"
            v-if="scope.row.progress !== '2'"
          >通过</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-message-solid"
            @click="handleInterview(scope.row)"
            v-hasPermi="['system:cv:notice']"
            v-if="scope.row.progress === '2'"
          >面试通知</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleRefuse(scope.row)"
            v-hasPermi="['system:cv:reject']"
            :disabled="scope.row.progress === '3'"
          >拒绝</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改简历筛选对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="招聘ID" prop="recruitId">
          <el-input v-model="form.recruitId" placeholder="请输入招聘ID" />
        </el-form-item>
        <el-form-item label="简历标题" prop="title">
          <el-input v-model="form.title" placeholder="请输入简历标题" />
        </el-form-item>
        <el-form-item label="简历" prop="file">
          <file-upload v-model="form.file"/>
        </el-form-item>
        <el-form-item label="进度" prop="progress">
          <el-select v-model="form.progress" placeholder="请选择进度">
            <el-option
              v-for="dict in dict.type.sys_cv_progress"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>


    <!-- 添加或修改面试记录对话框 -->
    <el-dialog :title="title" :visible.sync="interviewOpen" width="500px" append-to-body>
      <el-form ref="interviewForm" :model="interviewForm" label-width="80px">
        <el-form-item label="地点" prop="place">
          <el-input v-model="interviewForm.place" placeholder="请输入地点" />
        </el-form-item>
        <el-form-item label="面试时间" prop="time">
          <el-date-picker clearable
            v-model="interviewForm.time"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择面试时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="注意事项" prop="tips">
          <el-input v-model="interviewForm.tips" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="interviewForm.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitInterviewForm">确 定</el-button>
        <el-button @click="cancelInterview">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listCv, getCv, delCv, addCv, updateCv,getRecruitCvProgress } from "@/api/system/cv";
import {  addInterview, updateInterview } from "@/api/system/interview";
import * as echarts from 'echarts';

export default {
  name: "Cv",
  dicts: ['sys_recruit_progress','sys_cv_progress'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 简历筛选表格数据
      cvList: [],
      pieData: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 面试通知
      interviewOpen: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        recruitId: null,
        title: null,
        file: null,
        progress: null,
      },
      // 表单参数
      form: {},
      interviewForm: {},
      // 表单校验
      rules: {
        recruitId: [
          { required: true, message: "招聘ID不能为空", trigger: "blur" }
        ],
        title: [
          { required: true, message: "简历标题不能为空", trigger: "blur" }
        ],
        file: [
          { required: true, message: "简历不能为空", trigger: "blur" }
        ],
        progress: [
          { required: true, message: "进度不能为空", trigger: "change" }
        ],
        createBy: [
          { required: true, message: "创建人不能为空", trigger: "blur" }
        ],
      }
    };
  },
  mounted(){

    var pieDom = document.getElementById('cvBox_pie');
    var pieChart = echarts.init(pieDom);

    var lineDom = document.getElementById('cvBox_line');
    var lineChart = echarts.init(lineDom);

    const pie_option = {
      title: {
        text: '简历情况详情',
        left: 'center'
      },
      tooltip: {
        trigger: 'item'
      },
      legend: {
        orient: 'vertical',
        left: 'left'
      },
      series: [
        {
          name: 'Access From',
          type: 'pie',
          radius: '50%',
          data: [],
          emphasis: {
            itemStyle: {
              shadowBlur: 10,
              shadowOffsetX: 0,
              shadowColor: 'rgba(0, 0, 0, 0.5)'
            }
          }
        }
      ]
    };

    const line_option = {
        xAxis: {
          type: 'category',
          data: ['通过', '已投', '拒绝']
        },
        yAxis: {
          type: 'value'
        },
        series: [
          {
            data: [],
            type: 'bar',
            itemStyle: {
            color: function(params) {
              if (params.dataIndex === 1) {
                return 'green'; // 设置第二个柱为绿色
              } else if (params.dataIndex === 2) {
                return 'red'; // 设置第三个柱为红色
              } else {
                return 'blue'; // 设置其他柱的颜色，这里假设为蓝色
              }
            }
          }
          }
        ]
      };
      

    
    // 发起数据请求
    getRecruitCvProgress().then(res => {
      // 数据获取成功后设置图表的数据
      line_option.series[0].data = res.data.map(item => item.value);
      pie_option.series[0].data = res.data
      
      // 设置图表的选项
      lineChart.setOption(line_option);
      pieChart.setOption(pie_option);
    });

  },
  created() {
    this.$watch(
      ()=> this.$route.query.id,
      ()=>{
        this.getParmList()
      }
    )
    this.getParmList();
    // this.getList();
  },
  
  methods: {
    getParmList(){
      const id = this.$route.query.id
      this.queryParams.recruitId = id
      this.getList()
    },
    /** 查询简历筛选列表 */
    getList() {
      this.loading = true;
      listCv(this.queryParams).then(response => {
        this.cvList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 取消面试按钮
    cancelInterview() {
      this.interviewOpen = false;
      this.interviewReset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        recruitId: null,
        userId: null,
        title: null,
        file: null,
        progress: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    interviewReset() {
      this.form = {
        id: null,
        userId: null,
        recruitId: null,
        recruitCvId: null,
        place: null,
        time: null,
        tips: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("interviewForm");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加简历筛选";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getCv(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改简历筛选";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateCv(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCv(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 提交按钮 */
    submitInterviewForm() {
          
          if (this.interviewForm.id != null) {
            updateInterview(this.interviewForm).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.interviewOpen = false;
              this.getParmList()
            });
          } else {
            addInterview(this.interviewForm).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.interviewOpen = false;
              this.getParmList()
            });
          }
   
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除简历筛选编号为"' + ids + '"的数据项？').then(function() {
        return delCv(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 拒绝简历 */
    handleRefuse(row) {
      const id = row.id;
      this.$modal.confirm('是否确定拒绝"' + id + '"的简历？').then(() => {
        getCv(id).then(response => {
          this.form = response.data;
          this.form.progress = "3";
          updateCv(this.form).then(response => {
            this.$modal.msgSuccess("拒绝成功");
            this.getList();
          });
        });
      }).catch(() => {
        // 处理取消操作
      });
    },
    handleAccept(row){
      const id = row.id;
      this.$modal.confirm('是否确定通过"' + id + '"的简历？').then(() => {
        getCv(id).then(response => {
          this.form = response.data;
          this.form.progress = "2";
          updateCv(this.form).then(response => {
            this.$modal.msgSuccess("通过成功");
            this.getList();
          });
        });
      }).catch(() => {
        // 处理取消操作
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/cv/export', {
        ...this.queryParams
      }, `cv_${new Date().getTime()}.xlsx`)
    },
    handleInterview(row) {
      this.interviewOpen = true
      this.interviewForm.userId = row.userId
      this.interviewForm.recreitId = row.recruitId
      this.interviewForm.recruitCvId = row.id
      this.title = "发送面试通知";
      this.getParmList()
    }
  }
};
</script>
